package com.UdpRecieve.secret.log;

import com.UdpRecieve.domain.BusinessLog;
import com.UdpRecieve.secret.domain.SecretLableResult;
import com.UdpRecieve.tool.SpringUtils;
import com.UdpRecieve.tool.StringUtils;
import com.UdpRecieve.domain.*;
import com.UdpRecieve.mapper.BusinessLogMapper;
import com.UdpRecieve.mapper.FileCheckResultMapper;
import com.UdpRecieve.mapper.SysConfigMapper;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.Date;


/**
 * 归档逻辑中所使用的公共方法
 */
public class BussinessUtils {

    BusinessLogMapper businessLogMapper= SpringUtils.getBean(BusinessLogMapper.class);
    /*private SysConfigMapper systemConfigMapper =  SpringUtils.getBean(SysConfigMapper.class);
    private FileCheckResultMapper fileCheckResultMapper = SpringUtils.getBean(FileCheckResultMapper.class);*/
    /**
     * 封装完整的 filePigeonhole
     * @param filePigeonhole 文件对象
     * @param fileRealPath 加标文件路径
     * @param fileSourcePath 解标文件路径
     * @param now 当前时间
     * @return
     */
    public static FilePigeonhole installationF(FilePigeonhole filePigeonhole, String fileRealPath, String fileSourcePath, Date now){
        String fileSourceName = null;//原文件名
        if (!StringUtils.isEmpty(filePigeonhole.getFileName())&&filePigeonhole.getFileName().contains("_加标")){
            fileSourceName = filePigeonhole.getFileName().substring(0,filePigeonhole.getFileName().lastIndexOf("_加标"))+filePigeonhole.getFileType();
        }else{
            fileSourceName = filePigeonhole.getFileName();
        }
        filePigeonhole.setFileName(fileSourceName);

        filePigeonhole.setPigeonholeTime(DateFormatUtils.format(now,"yyyy-MM-dd HH-mm-ss"));//归档时间
        return filePigeonhole;
    }

    /**
     * 618的压缩包 在 (落地 和 md5)校验失败后
     * 存储本地日志的方法
     * @param filePigeonhole 归档对象
     * @param secretLableResult 结果 对象
     * @param now 时间
     */

    public void  filePigeonholeLogSave618FM(FilePigeonhole filePigeonhole, SecretLableResult secretLableResult,Date now){
        //落地结果
        int save_result = secretLableResult.getSave_result()?1:0;
        //拷贝给单导
        int copy_single = secretLableResult.getCopy_single()?1:0;
        String result = save_result + "_" + copy_single;
        BusinessLog businessLog = new BusinessLog();
        businessLog.setFileName(filePigeonhole.getFileName());
        businessLog.setFileNumber(filePigeonhole.getFileNumber());
        businessLog.setBusinessMd5(filePigeonhole.getMd5CheckNum());
        businessLog.setBusinessTime(DateFormatUtils.format(now,"yyyy-MM-dd HH:mm:ss"));//当前时间
        businessLog.setBusinessResult(result);
        businessLog.setFileApproveTime(filePigeonhole.getApproverTime());//添加审批时间
        businessLogMapper.insertBusinessLog(businessLog);
    }

    /**
     * 618的压缩包 在 (落地 和 md5)校验失败后
     * 存储本地日志的方法
     * @param filePigeonhole 归档对象
     * @param secretLableResult 结果 对象
     * @param now 时间
     */
    public void save618Zip(FilePigeonhole filePigeonhole,SecretLableResult secretLableResult,Date now){

        int save_result = secretLableResult.getSave_result()?1:0;
        int level = secretLableResult.getSecret_level_isOk();
        int status = secretLableResult.getSecret_status_isOk();
        int extend = secretLableResult.getSecret_extend_isOk();
        int drafer = secretLableResult.getSecret_drafter_isOk();
        int authority = secretLableResult.getSecret_authority_isOk();
        int check_result = secretLableResult.getSecret_result()?1:0;
        int secret_solve_result = secretLableResult.getSecret_solve_result()?1:0;
        int copy_single = secretLableResult.getCopy_single()?1:0;

        String result = save_result+"_"+level+"_"+status+"_"
                +extend+"_"+drafer+"_"+authority+"_"+check_result+"_"
                +secret_solve_result+"_"+copy_single;

        BusinessLog businessLog = new BusinessLog();
        businessLog.setFileName(filePigeonhole.getFileName());
        businessLog.setBusinessMd5("失败");
        businessLog.setBusinessTime(DateFormatUtils.format(now,"yyyy-MM-dd HH:mm:ss"));//当前时间
        businessLog.setBusinessResult(result);
        businessLog.setFileApproveTime(filePigeonhole.getApproverTime());//添加审批时间
        //如果是压缩包就循环fileNames字段
        if(filePigeonhole.getFileType().contains("rar")||filePigeonhole.getFileType().contains("zip")){
            for(String fileName:filePigeonhole.getFileNames()){
                businessLog.setFileName(fileName);
                businessLogMapper.insertBusinessLog(businessLog);
            }
        }else {
            businessLogMapper.insertBusinessLog(businessLog);
        }
    }


    /**
     * 存储本地日志的方法
     * @param filePigeonhole 归档对象
     * @param secretLableResult 结果 对象
     * @param now 时间
     */
    public void save(FilePigeonhole filePigeonhole,SecretLableResult secretLableResult,Date now){
        int save_result = secretLableResult.getSave_result()?1:0;
        int level = secretLableResult.getSecret_level_isOk();
        int status = secretLableResult.getSecret_status_isOk();
        int extend = secretLableResult.getSecret_extend_isOk();
        int drafer = secretLableResult.getSecret_drafter_isOk();
        int authority = secretLableResult.getSecret_authority_isOk();
        int check_result = secretLableResult.getSecret_result()?1:0;
        int secret_solve_result = secretLableResult.getSecret_solve_result()?1:0;
        int copy_single = secretLableResult.getCopy_single()?1:0;

        String result = save_result+"_"+level+"_"+status+"_"
                +extend+"_"+drafer+"_"+authority+"_"+check_result+"_"
                +secret_solve_result+"_"+copy_single;

        BusinessLog businessLog = new BusinessLog();
        businessLog.setFileName(filePigeonhole.getFileName());
        businessLog.setBusinessMd5(filePigeonhole.getMd5CheckNum()==null?"失败":filePigeonhole.getMd5CheckNum());
        businessLog.setBusinessTime(DateFormatUtils.format(now,"yyyy-MM-dd HH:mm:ss"));//当前时间
        businessLog.setBusinessResult(result);//结果-1成功,0失败
        businessLog.setFileNumber(filePigeonhole.getFileNumber());
        businessLogMapper.insertBusinessLog(businessLog);
    }
}
